const express = require('express')
const body = require('express-body')

const db = require('./database')

const app = express()

app.use(body);

/*
    曾
    删
    改
    查
*/

// 查
app.get('/employee', (req, res) => {
    console.log('get employee')
    res.send(db.query())
})
// 修改
app.post('/employee', (req, res) => {
    const data = req.body
    console.log('修改', data);
    db.update(data);
    res.send({
        success: true,
        msg: ''
    })
})
// 删除
app.delete('/employee', (req, res) => {
    const data = req.body
    console.log('删除', data);
    db.delete(data);
    res.send({
        success: true,
        msg: ''
    })
})
// 增加
app.put('/employee', (req, res) => {
    const data = req.body
    console.log('增加', data);

    data.id = parseInt(Math.random() * 999)
    data.key = data.id

    db.push(data);
    res.send({
        success: true,
        msg: ''
    })
})

app.listen(3002)
console.log('server is runing http://localhost:3002');

